package com.yanqu.road.server.cmd.beautyescort;

import com.yanqu.road.entity.beautyescort.EscortBeautyDispatch;
import com.yanqu.road.entity.beautyescort.EscortCartData;
import com.yanqu.road.entity.beautyescort.enums.eEscortCartState;
import com.yanqu.road.pb.beautyescort.BeautyEscortProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.manager.beautyescort.CrossBeautyEscortMgr;
import com.yanqu.road.server.manager.beautyescort.CrossEscortPlayerWrap;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;

import java.util.Map;

@Cmd(code = Protocol.C_BEAUTY_ESCORT_SYNC_PLAY_INFO, desc = "同步玩家信息")
public class CrossBeautyEscortSyncPlayerInfoCmd extends CrossCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        BeautyEscortProto.CrossBeautyEscortPlayInfoSyncMsg syncMsg = BeautyEscortProto.CrossBeautyEscortPlayInfoSyncMsg.parseFrom(packet.getBytes());
        for (BeautyEscortProto.CrossBeautyEscortPlayInfoTemp playInfoTemp : syncMsg.getInfoList()) {
            CrossEscortPlayerWrap playerWrap = CrossBeautyEscortMgr.getEscortPlayer(serverId, playInfoTemp.getUserId());
            if(playerWrap != null){
                playerWrap.getUserData().setTotalGlamour(playInfoTemp.getTotalGlamour());
                //走镖数据
                for (Map.Entry<Integer, EscortCartData> dataEntry : playerWrap.getCartDataMap().entrySet()) {
                    EscortCartData cartData = dataEntry.getValue();
                    if(cartData.getState() == eEscortCartState.Escorting.getIntValue()){
                        synchronized (cartData){
                            boolean change = false;
                            if(cartData.getState() == eEscortCartState.Escorting.getIntValue()){
                                for (EscortBeautyDispatch dispatch : cartData.getBeautyDispatchList()) {
                                    for (BeautyEscortProto.BeautyEscortBeautyDispatchDataTemp dataTemp : playInfoTemp.getBeautyList()) {
                                        if(dispatch.getBeautyId() == dataTemp.getBeautyId() && dispatch.getGlamour() != dataTemp.getGlamour()){
                                            dispatch.setGlamour(dataTemp.getGlamour());
                                            change = true;
                                            break;
                                        }
                                    }
                                }
                            }
                            if(change){
                                cartData.setUpdateOption();
                            }
                        }
                    }
                }
                //log.info("同步玩家{}信息  totalGlamour:{}", playerWrap.getUserData().getUserId(), playInfoTemp.getTotalGlamour());
            }
        }
    }

    @Override
    public void sendData() {

    }
}
